/*
	Check lien tuc ac thong bao hay event
	Cho phep tuong tac voi nguoi dung bang popup
*/

Owl(function( $ ){
	function showLabel( n ){
		if( n == 0 ){
			$("#toolbar .tool-notice")
				.empty("span");
			return;
		}
		$("#toolbar .tool-notice")
			.empty("span")
			.css({position:'relative'})
			.append("<span id='tool-num'>"+n+"</span>")	
			
		$("#tool-num").animate({
			rotate:360			
		},{
			duration:200,
			easing:'swing'
		})	
	}

	function showNotice( a ){
		$("#noticesound")
			.each(function(){   this.play() });	
			
		var cc = (  a.created_by_photo ? 
			"<img style='margin-right:2px;width:60px;height:60px;padding:1;border:1px solid #ccc;float:left' src='"+baseURL+"/files/photo/"+a.created_by_photo+"'/>"
			: ("<img style='margin-right:2px;width:60px;height:60px;padding:1;border:1px solid #ccc;float:left' src='"+baseURL+"/files/photo/noavatar.gif'/>"))
			+"<a href='"+a.url+"'>"+a.title+"</a>"
			+"<br/>"+a.content;
		
		$("<div>"
			+"<div style='position:relative;font-size:13px;font-weight:bold;background:#3B5999;padding:8px 0px;color:#fff'>Thông báo mới"
			+"<a class='close' style='background:url("+baseURL+"/style/images/close.gif);display:inline-block;width:19px;height:19px;position:absolute;right:0px;top:9px'></a>"
			+"</div><div style='background:#fff;width:250px;height:70px;overflow:auto;padding:8px 3px'><div class='ncontent'>"+cc+"</div></div>"
			+"</div>")	
			.appendTo('body')
			.css({
				position:'fixed',
				border: '6px solid #3B5999',
				borderTop:'0px',
				overflow:'hidden',
				background:'#fff',
				'z-index':2000,
				bottom:'-500px',
				right:'0px'					
			})
			.animate({
				bottom:'1px'
			},{
				duration: 600,
				easing: 'outCubic'
			})
			.each(function(){
				var elem = this;
				$(this)
					.find(".close")
					.onClick(function(){
						clearTimeout(this.tt);
						$(elem)
							.stop()
							.animate({
								bottom: '-500px'
							},{
								duration: 500,
								easing: 'inCubic',
								callback: function(){
									$(this).remove()
								}	
							})
					})
					.each(function(){
						var e = this;
						this.tt = setTimeout(function(){
							$(e).fire("click")
						},7000);
					});
					
			});		
	}
	
	function getNotice(){
		var self = arguments.callee;
		$.Ajax(baseURL+"/Notice/Index/Check",{
			cache: false,
			error: function(){
				setTimeout(self,10000);
			},
			complete: function(){
				setTimeout(self,10000);
			},
			success: function( data ){				
				try{
					eval("data="+data);
				}catch(e){
					return false;
				}	
				showLabel( data['num']||0 );
				
				if( data['post'] ){
					
					showNotice( data['post'] );
				}	
								
			}
		});
	}

	setTimeout(getNotice,5000);	
});